"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useImmer = useImmer;
exports.useImmerReducer = useImmerReducer;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _immer = _interopRequireDefault(require("immer"));
/**
 * 引用对象不被修改，引用对象每被修改则返回一个新的对象
 * @param initialValue
 */
function useImmer(initialValue) {
  var _useState = (0, _react.useState)(initialValue),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    val = _useState2[0],
    setVal = _useState2[1];
  return [val, (0, _react.useCallback)(function (updater) {
    if (typeof updater === 'function') {
      return setVal((0, _immer.default)(updater));
    }
    return setVal(updater);
  }, [])];
}
function useImmerReducer(reducer, initialState, initialAction) {
  var cachedReducer = (0, _react.useMemo)(function () {
    return (0, _immer.default)(reducer);
  }, [reducer]);
  return (0, _react.useReducer)(cachedReducer, initialState,
  // @ts-ignore
  initialAction);
}